//
//  UploadRaiseInfoOrgViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/8/3.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "UploadRaiseInfoOrgViewController.h"
#import "ItemDocument.h"
#import "UploadImageViewController.h"
@interface UploadRaiseInfoOrgViewController ()
@property (nonatomic,weak) IBOutlet UILabel *otherInfoLab;
@property (nonatomic,weak) IBOutlet UIButton *otherInfoBtn;

@property (nonatomic,weak) IBOutlet UILabel *baseInfoLab;
@property (nonatomic,weak) IBOutlet UIButton *baseInfoBtn;

@property (nonatomic,weak) IBOutlet UILabel *businessConditionsInfoLab;
@property (nonatomic,weak) IBOutlet UIButton *businessConditionsInfoBtn;

@property (nonatomic,weak) IBOutlet UILabel *creditProofLab;
@property (nonatomic,weak) IBOutlet UIButton *creditProofBtn;

@property (nonatomic,weak) IBOutlet UILabel *relevantLawInfoLab;
@property (nonatomic,weak) IBOutlet UIButton *relevantLawInfoBtn;

@property (nonatomic,weak) IBOutlet UILabel *assetPropertyProofLab;
@property (nonatomic,weak) IBOutlet UIButton *assetPropertyProofBtn;

@property (nonatomic,weak) IBOutlet UILabel *financeReportInfoLab;
@property (nonatomic,weak) IBOutlet UIButton *financeReportInfoBtn;

@property (nonatomic,assign) int documentType;
@end

@implementation UploadRaiseInfoOrgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"上传项目资料"];
    self.baseInfoBtn.tag = BaseInfo;
    [self.baseInfoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.businessConditionsInfoBtn.tag = BusinessConditionsInfo;
    [self.businessConditionsInfoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];

    self.creditProofBtn.tag = CreditProof;
    [self.creditProofBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.relevantLawInfoBtn.tag = RelevantLawInfo;
    [self.relevantLawInfoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.assetPropertyProofBtn.tag = AssetPropertyProof;
    [self.assetPropertyProofBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.financeReportInfoBtn.tag = FinanceReportInfo;
    [self.financeReportInfoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.otherInfoBtn.tag = OtherAddInfo;
    [self.otherInfoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([self.baseInfoImgUrlArray count]>0){
        self.baseInfoLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.baseInfoImgUrlArray count]];
    }else{
        self.baseInfoLab.text = @"未上传文件";
    }
    if ([self.businessConditionInfoUrlArray count]>0){
        self.businessConditionsInfoLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.businessConditionInfoUrlArray count]];
    }else{
        self.businessConditionsInfoLab.text = @"未上传文件";
    }
    if ([self.assetPropertyProofUrlArray count]>0){
        self.assetPropertyProofLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.assetPropertyProofUrlArray count]];
    }else{
        self.assetPropertyProofLab.text = @"未上传文件";
    }
    if ([self.financeReportInfoUrlArray count]>0){
        self.financeReportInfoLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.financeReportInfoUrlArray count]];
    }else{
        self.financeReportInfoLab.text = @"未上传文件";
    }
    if ([self.creditProofUrlArray count]>0){
        self.creditProofLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.creditProofUrlArray count]];
    }else{
        self.creditProofLab.text = @"未上传文件";
    }
    if ([self.relevantLawUrlArray count]>0){
        self.relevantLawInfoLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.relevantLawUrlArray count]];
    }else{
        self.relevantLawInfoLab.text = @"未上传文件";
    }
    if ([self.otherInfoUrlArray count]>0){
        self.otherInfoLab.text = [NSString stringWithFormat:@"已上传%ld个文件",[self.otherInfoUrlArray count]];
    }else{
        self.otherInfoLab.text = @"未上传文件";
    }
    
}
#pragma mark - custom method
-(void)uploadImage:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    switch (btn.tag) {
        case BaseInfo:
            [self configerationWithLabel:self.baseInfoLab array:self.baseInfoImgUrlArray Button:self.baseInfoBtn ids:self.baseInfoImgIdArray];
            break;
        case BusinessConditionsInfo:
            [self configerationWithLabel:self.businessConditionsInfoLab array:self.businessConditionInfoUrlArray Button:self.businessConditionsInfoBtn ids:self.businessConditionInfoIdArray];
            break;
        case AssetPropertyProof:
            [self configerationWithLabel:self.assetPropertyProofLab array:self.assetPropertyProofUrlArray Button:self.assetPropertyProofBtn ids:self.assetPropertyProofIdArray];
            break;
        case FinanceReportInfo:
            [self configerationWithLabel:self.financeReportInfoLab array:self.financeReportInfoUrlArray Button:self.financeReportInfoBtn ids:self.financeReportInfoIdArray];
            break;
        case CreditProof:
            [self configerationWithLabel:self.creditProofLab array:self.creditProofUrlArray Button:self.creditProofBtn ids:self.creditProofIdArray];
            break;
        case RelevantLawInfo:
            [self configerationWithLabel:self.relevantLawInfoLab array:self.relevantLawUrlArray Button:self.relevantLawInfoBtn ids:self.relevantLawIdArray];
            break;
        case OtherAddInfo:
            [self configerationWithLabel:self.otherInfoLab array:self.otherInfoUrlArray Button:self.otherInfoBtn ids:self.otherInfoIdArray];
            break;
        default:
            break;
    }
    
}

-(void)configerationWithLabel:(UILabel *)label array:(NSMutableArray *)array Button:(UIButton *)button ids:(NSMutableArray *)myIds
{
    UploadImageViewController *uploadImageVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"UploadImageViewController"];
    uploadImageVC.documentTypeName = button.tag;
    uploadImageVC.imageUrlArray = [NSMutableArray array];
    uploadImageVC.imageIdArray = [NSMutableArray array];
    [uploadImageVC.imageUrlArray addObjectsFromArray:array];
    [uploadImageVC.imageIdArray addObjectsFromArray:myIds];
    
    uploadImageVC.uploadFinish = ^(NSMutableArray *urlArray,NSMutableArray *ids){
        NSLog(@"回调了");
        NSLog(@"array=%ld",[urlArray count]);
        NSLog(@"前base=%ld",[array count]);
        [array removeAllObjects];
        [myIds removeAllObjects];
        [array addObjectsFromArray:urlArray];
        [myIds addObjectsFromArray:ids];
        NSLog(@"后base=%ld",[array count]);
        if ([array count]>0){
            label.text = [NSString stringWithFormat:@"已上传%ld个文件",[array count]];
        }else{
            label.text = @"未上传文件";
        }
        [self UploadFinish];
    };
    [self displayViewController:uploadImageVC];
}
-(void)UploadFinish
{
    Boolean imgIsEnough = true;
    if (![self.baseInfoImgUrlArray count] || ![self.assetPropertyProofUrlArray count] || ![self.creditProofUrlArray count] || ![self.relevantLawUrlArray count] || ![self.businessConditionInfoUrlArray count] || ![self.financeReportInfoUrlArray count]) {
        imgIsEnough = false;
    }
    self.uploadFinish(self.baseInfoImgUrlArray,self.businessConditionInfoUrlArray,self.creditProofUrlArray,self.assetPropertyProofUrlArray,self.financeReportInfoUrlArray,self.relevantLawUrlArray,self.otherInfoUrlArray,imgIsEnough);
    self.uploadImageId(self.baseInfoImgIdArray,self.businessConditionInfoIdArray,self.creditProofIdArray,self.assetPropertyProofIdArray,self.financeReportInfoIdArray,self.relevantLawIdArray,self.otherInfoIdArray);
    
}

@end
